package com.runfast.waimai.web.controller.Runfastbusinesscontroller;



import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.model.BaseUser;
import com.runfast.waimai.dao.model.RunfastGoodsSellRecord;
import com.runfast.waimai.dao.model.WaiMaiRunfastAgentaccounts;
import com.runfast.waimai.service.RunfastDistributionassessService;
import com.runfast.waimai.service.RunfastGoodsSellRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/* 统计团队下骑手的差评问题
 *autor mengxiang
 * time 2018/6/4
 * */
@RestController
@RequestMapping("/server/shopper/*")
public class ShopperErrController {
    @Resource
    RunfastGoodsSellRecordService runfastGoodsSellRecordService;
    @Resource
    RunfastDistributionassessService runfastDistributionassessService;

    //  @PostMapping("error")// 统计团队下骑手的差评问题
    @GetMapping("" +
            "error")
    public @ResponseBody ModelMap error(String cityname, String shoppername, String createtime1, String createtime2, String agentid,
                                        String a,
                                        @RequestParam(value = "page",required =false,defaultValue = "0")Integer page,
                                        @RequestParam(value = "pagesize",required = false,defaultValue = "10")Integer pagesize){

        Map<Object,Object> map1=new HashMap<Object, Object>();
        List<String>agentids=new ArrayList<>();

        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();

        if(admin!=null && admin.getType()==2 && agentAccounts!=null){ //代理商登陸
            agentid= String.valueOf( agentAccounts.getAgentid());
        }
        System.out.println(agentid+"agentid***************************************");
        //cityname为城市名，shoppername为骑手名， createatime1为开始时间， createatime2 为结束时间 ，Interger errend为已完成取消定单 1 可以不写，
        //a=1 最近一天，a=2 最近七天， a=3 最近一个月 a为其它值执行搜索查询
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        ModelMap model=new ModelMap();
        //    RunfastShoppererror shoppererror=new RunfastShoppererror();
        // 要显示 代理商 配送员名称 有效完成单 配送所得金额   超时单，超时率，异常取消单， 异常取消率 差评数， 差评率
        Integer a1=Integer.parseInt(a);
        Integer page1=page*pagesize;
        System.out.println(a1);

        try {
            Date date1=format.parse(createtime1+" 00:00:00");
            Date date2=format.parse(createtime2+" 23:59:59");
            Date date3=new Date();//系统当前时间
            Calendar Cal=Calendar.getInstance();
            Cal.setTime(date3);


            if(agentid==null||agentid==""){
                List<Map<String,BigDecimal>>listmap= runfastGoodsSellRecordService.findshopperAndCityAndtime(cityname,shoppername,date1,date2,page1,pagesize,null);
                Integer count=runfastGoodsSellRecordService.findshopperAndCityAndtimecount(cityname,shoppername,date1,date2,page1,pagesize,null);
                if(null==listmap){
                    model.put("msg","亲，没有你要的数据");
                    model.put("success",false);
                }else {

                    model.put("listmap",listmap);
                    model.put("count",count);

                    model.put("success",true);
                }


            }else {
                List<String> result = Arrays.asList(agentid.split(","));
                List<Integer>list=new ArrayList<>();
                for(String x:result){
                    list.add(Integer.parseInt(x));
                }

                    List<Map<String,BigDecimal>>listmap= runfastGoodsSellRecordService.findshopperAndCityAndtime(cityname,shoppername,date1,date2,page1,pagesize,list);
                    Integer count=runfastGoodsSellRecordService.findshopperAndCityAndtimecount(cityname,shoppername,date1,date2,page1,pagesize,list);

                if(null==listmap){
                    model.put("msg","亲，没有你要的数据");
                    model.put("success",false);
                }else {

                    model.put("listmap",listmap);
                    model.put("count",count);

                    model.put("success",true);
                }
            }




 /*       if(a1==1){
                Cal.add(Calendar.HOUR_OF_DAY,-24);
                List<Map<String,BigDecimal>>listmap= runfastGoodsSellRecordService.findshopperAndCityAndtime(cityname,shoppername,Cal.getTime(),date3,page1,pagesize,Integer.parseInt(agentid));
                if(null==listmap){
                    model.put("msg","亲，没有你要的数据");
                    model.put("success",false);
                }else {

                    model.put("listmap",listmap);
                    model.put("success",true);

                }
            }else  if(a1==2){
                Cal.add(Calendar.HOUR_OF_DAY,-24*7);
                List<Map<String,BigDecimal>>listmap= runfastGoodsSellRecordService.findshopperAndCityAndtime(cityname,shoppername,Cal.getTime(),date3,page1,pagesize,Integer.parseInt(agentid));
                if(null==listmap){
                    model.put("msg","亲，没有你要的数据");
                    model.put("success",false);
                }else {

                    model.put("listmap",listmap);
                    model.put("success",true);

                }
            }else if(a1==3){
                Cal.add(Calendar.HOUR_OF_DAY,-24*30);
                List<Map<String,BigDecimal>>listmap= runfastGoodsSellRecordService.findshopperAndCityAndtime(cityname,shoppername,Cal.getTime(),date3,page1,pagesize,Integer.parseInt(agentid));
                if(null==listmap){
                    model.put("msg","亲，没有你要的数据");
                    model.put("success",false);
                }else{

                    model.put("listmap",listmap);
                    model.put("success",true);

                }
            } else if(a1==4) {
                List<Map<String,BigDecimal>>listmap= runfastGoodsSellRecordService.findshopperAndCityAndtime(cityname,shoppername,date1,date2,page1,pagesize,Integer.parseInt(agentid));
                Integer count=runfastGoodsSellRecordService.findshopperAndCityAndtimecount(cityname,shoppername,date1,date2,page1,pagesize,Integer.parseInt(agentid));
                if(null==listmap){
                    model.put("msg","亲，没有你要的数据");
                    model.put("success",false);
                }else {

                    model.put("listmap",listmap);
                    model.put("count",count);
                    model.put("date1",date1);
                    model.put("date2",date2);
                    model.put("success",true);
                }
            }else {
                model.put("success",false);
                model.put("msg","系统错误");
            }
*/

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  model;
    }
    /*
     * 超时单查询
     * */
    //  @PostMapping("aftertime")
    @GetMapping("aftertime")
    public @ResponseBody ModelMap aftertime(String createatime1,String createatime2,String cityname,String shoppername){
        ModelMap model=new ModelMap();
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        try {
            Date date1=dateFormat.parse(createatime1+" 00:00:00");
            Date date2=dateFormat.parse(createatime2+" 23:59:59");
            List<RunfastGoodsSellRecord>list= runfastGoodsSellRecordService.findaftertime(date1,date2,cityname,shoppername);
            if(null==list){ model.put("msg","没有超时的定单，你太棒了");
                model.put("success",false);
            }else {model.put("list",list);
                model.put("success",true);}
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  model;
    }
    /*
     * 异常取消单
     * */
    //  @PostMapping("errorOver")
    @GetMapping("errorOver")
    public @ResponseBody ModelMap errorOver(String createatime1,String createatime2,String cityname,String shoppername){
        ModelMap model=new ModelMap();
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1=dateFormat.parse(createatime1);
            Date date2=dateFormat.parse(createatime2);
            List<RunfastGoodsSellRecord>list= runfastGoodsSellRecordService.finderrorOver(date1,date2,cityname,shoppername);
            if(null==list){ model.put("msg","没有异常取消单");
                model.put("success",false);
            }else {model.put("list",list);
                model.put("success",true);}
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  model;
    }
    /*
     * 差评单
     * */
    //   @PostMapping("lastscore")
    @GetMapping("lastscore")
    public @ResponseBody ModelMap lastscore(String createatime1,String createatime2,String cityname,String shoppername){
        ModelMap model=new ModelMap();
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1=dateFormat.parse(createatime1);
            Date date2=dateFormat.parse(createatime2);
            List<RunfastGoodsSellRecord>list= runfastGoodsSellRecordService.findlastscore(date1,date2,cityname,shoppername);
            if(null==list){ model.put("msg","没有差评单");
                model.put("success",false);
            }else {model.put("list",list);
                model.put("success",true);}
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  model;
    }

}
